package com.ymatou.iapi.shoppingcartservice.testcase;

import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.shoppingcartservice.parameter.ShoppingcartProductBean;
import com.ymatou.iapi.shoppingcartservice.parameter.ShoppingcartProductUpdateCatalogIdReq;
import com.ymatou.iapi.shoppingcartservice.service.ShoppingcartProductUpdateCatalogIdCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.BuyerShoppingCartService;
import com.ymttest.database.mongo.YmtShoppingCartWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.MapUtil;

public class Ts_ShoppingcartProductUpdateCatalogId {
	
	private static ShoppingcartProductUpdateCatalogIdCall shoppingcartProductUpdateCatalogIdCall = new ShoppingcartProductUpdateCatalogIdCall();
	private static Integer userId = Integer.valueOf(EnvSetup.getData("shoppingcartUserId"));
	private static BuyerShoppingCartService buyerShoppingCartService = new BuyerShoppingCartService();

	YmtShoppingCartWapper ymtShoppingCartWapper = new YmtShoppingCartWapper();
	
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("更新商品catalogId");
	}
	
	@Before
	public void caseUp() {
	}
	
	@AfterClass
	public static void tearDown() {
		Logger.generateResult(shoppingcartProductUpdateCatalogIdCall.getOpurl());
	}	
	
	@After
	public void caseDown() {
		Logger.end();
		try {
			ymtShoppingCartWapper.deleteByUserId(userId);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ShoppingcartProductUpdateCatalogId_001() throws Exception {
		Logger.start(true,"更新商品信息");
		try{
			String shoppingCartId = buyerShoppingCartService.AddShoppingCartProduct(userId);
			ShoppingcartProductUpdateCatalogIdReq req = new ShoppingcartProductUpdateCatalogIdReq();
			
			int num = 20;
			String version = "2.002";
			String catalogId = "update-catalogId";
			int deliveryType = 13;
			
			req.setShoppingCartId(shoppingCartId);
			req.setPurchaseNum(num);
			req.setVersion(version);
			req.setUserId(userId);
			req.setCatalogId(catalogId);
			req.setDeliveryType(deliveryType);
			
			JSONObject ret = shoppingcartProductUpdateCatalogIdCall.callAndGetReturnData(req);
			int code = ret.getInt("code");
			
			Logger.verifyEquals(200, code, "接口code返回异常");
			
			ShoppingcartProductBean shoppingcartProductBean = ShoppingcartProductBean.getAddShoppingCartRequestFromFile("addshoppingcartcalltpl", 
					MapUtil.hashMap("userId", userId,
							"num", num,
							"version", version,
							"catalogId", catalogId,
							"deliveryType", deliveryType,
							"shoppingCartId", shoppingCartId));
			
			buyerShoppingCartService.checkShoppingcartProductBeanFromDB(shoppingcartProductBean);
			
		}catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ShoppingcartProductUpdateCatalogId_002() throws Exception {
		Logger.start(true,"更新商品信息, 不传入deliveryType,默认为0");
		try{
			String shoppingCartId = buyerShoppingCartService.AddShoppingCartProduct(userId);
			ShoppingcartProductUpdateCatalogIdReq req = new ShoppingcartProductUpdateCatalogIdReq();
			
			int num = 20;
			String version = "2.002";
			String catalogId = "update-catalogId";
			
			req.setShoppingCartId(shoppingCartId);
			req.setPurchaseNum(num);
			req.setVersion(version);
			req.setUserId(userId);
			req.setCatalogId(catalogId);
			
			JSONObject ret = shoppingcartProductUpdateCatalogIdCall.callAndGetReturnData(req);
			int code = ret.getInt("code");
			
			Logger.verifyEquals(200, code, "接口code返回异常");
			
			ShoppingcartProductBean shoppingcartProductBean = ShoppingcartProductBean.getAddShoppingCartRequestFromFile("addshoppingcartcalltpl", 
					MapUtil.hashMap("userId", userId,
							"num", num,
							"version", version,
							"catalogId", catalogId,
							"deliveryType",0,
							"shoppingCartId", shoppingCartId));
			
			buyerShoppingCartService.checkShoppingcartProductBeanFromDB(shoppingcartProductBean);
			
		}catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ShoppingcartProductUpdateCatalogId_003() throws Exception {
		Logger.start(true,"更新商品信息，不传入shoppingcartId");
		try{
			buyerShoppingCartService.AddShoppingCartProduct(userId);
			ShoppingcartProductUpdateCatalogIdReq req = new ShoppingcartProductUpdateCatalogIdReq();
			
			int num = 20;
			String version = "2.002";
			String catalogId = "update-catalogId";
			int deliveryType = 13;
			
			req.setShoppingCartId("");
			req.setPurchaseNum(num);
			req.setVersion(version);
			req.setUserId(userId);
			req.setCatalogId(catalogId);
			req.setDeliveryType(deliveryType);
			
			JSONObject ret = shoppingcartProductUpdateCatalogIdCall.callAndGetReturnData(req);
			int code = ret.getInt("code");
			String message = ret.getString("message");
			
			Logger.verifyEquals(500, code, "接口code返回异常");
			Logger.verifyEquals("shoppingCartId:shoppingCartId不能为空;", message, "不传入shoppingcartId提示错误");
			
		}catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ShoppingcartProductUpdateCatalogId_004() throws Exception {
		Logger.start(true,"更新商品信息,不传入num");
		try{
			String shoppingCartId = buyerShoppingCartService.AddShoppingCartProduct(userId);
			ShoppingcartProductUpdateCatalogIdReq req = new ShoppingcartProductUpdateCatalogIdReq();
			
			String version = "2.002";
			String catalogId = "update-catalogId";
			int deliveryType = 13;
			
			req.setShoppingCartId(shoppingCartId);
			req.setPurchaseNum(0);
			req.setVersion(version);
			req.setUserId(userId);
			req.setCatalogId(catalogId);
			req.setDeliveryType(deliveryType);
			
			JSONObject ret = shoppingcartProductUpdateCatalogIdCall.callAndGetReturnData(req);
			int code = ret.getInt("code");
			String message = ret.getString("message");
			
			Logger.verifyEquals(500, code, "接口code返回异常");
			Logger.verifyEquals("purchaseNum:错误的purchaseNum;", message, "不传入num提示错误");
			
		}catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ShoppingcartProductUpdateCatalogId_005() throws Exception {
		Logger.start(true,"更新商品信息,不传入version");
		try{
			String shoppingCartId = buyerShoppingCartService.AddShoppingCartProduct(userId);
			ShoppingcartProductUpdateCatalogIdReq req = new ShoppingcartProductUpdateCatalogIdReq();
			
			int num = 20;
			String version = "";
			String catalogId = "update-catalogId";
			int deliveryType = 13;
			
			req.setShoppingCartId(shoppingCartId);
			req.setPurchaseNum(num);
			req.setVersion(version);
			req.setUserId(userId);
			req.setCatalogId(catalogId);
			req.setDeliveryType(deliveryType);
			
			JSONObject ret = shoppingcartProductUpdateCatalogIdCall.callAndGetReturnData(req);
			int code = ret.getInt("code");
			String message = ret.getString("message");
			
			Logger.verifyEquals(500, code, "接口code返回异常");
			Logger.verifyEquals("version:version不能为空;", message, "不传入version提示错误");
			
			
		}catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ShoppingcartProductUpdateCatalogId_006() throws Exception {
		Logger.start(true,"更新商品信息,不传入userid");
		try{
			String shoppingCartId = buyerShoppingCartService.AddShoppingCartProduct(userId);
			ShoppingcartProductUpdateCatalogIdReq req = new ShoppingcartProductUpdateCatalogIdReq();
			
			int num = 20;
			String version = "2.002";
			String catalogId = "update-catalogId";
			int deliveryType = 13;
			
			req.setShoppingCartId(shoppingCartId);
			req.setPurchaseNum(num);
			req.setVersion(version);
			req.setUserId(userId);
			req.setCatalogId(catalogId);
			req.setDeliveryType(deliveryType);
			
			JSONObject ret = shoppingcartProductUpdateCatalogIdCall.callAndGetReturnData(req);
			int code = ret.getInt("code");
			String message = ret.getString("message");
			
			Logger.verifyEquals(500, code, "接口code返回异常");
			Logger.verifyEquals("userId:must be greater than or equal to 1;", message, "不传入userid提示错误");
			
		}catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ShoppingcartProductUpdateCatalogId_007() throws Exception {
		Logger.start(true,"更新商品信息，不传入catalogid");
		try{
			String shoppingCartId = buyerShoppingCartService.AddShoppingCartProduct(userId);
			ShoppingcartProductUpdateCatalogIdReq req = new ShoppingcartProductUpdateCatalogIdReq();
			
			int num = 20;
			String version = "2.002";
			String catalogId = "";
			int deliveryType = 13;
			
			req.setShoppingCartId(shoppingCartId);
			req.setPurchaseNum(num);
			req.setVersion(version);
			req.setUserId(userId);
			req.setCatalogId(catalogId);
			req.setDeliveryType(deliveryType);
			
			JSONObject ret = shoppingcartProductUpdateCatalogIdCall.callAndGetReturnData(req);
			int code = ret.getInt("code");
			String message = ret.getString("message");
			
			Logger.verifyEquals(500, code, "接口code返回异常");
			Logger.verifyEquals("catalogId:catalogId不能为空;", message, "不传入catalogId提示错误");
			
		}catch (Exception e) {
			Logger.fail(e);
		}
	}
}
